package com.cs.pk.view;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.part.ViewPart;

import com.cs.pk.view.tab.ShellViewTab;

public class ShellView extends ViewPart implements ISelectionListener,
		SelectionListener, IMenuListener {

	private ShellViewTab tabFolder;

	private PageBook pagebook;

	private Label noTabShownLabel;

	public ShellView() {
		// TODO Auto-generated constructor stub
	}

	public void createPartControl(Composite parent) {
		pagebook = new PageBook(parent, SWT.NONE);
		tabFolder = new ShellViewTab(pagebook, this);
		tabFolder.getFolder().addSelectionListener(this);

		// Page 2: Nothing selected
		noTabShownLabel = new Label(pagebook, SWT.TOP + SWT.LEFT + SWT.WRAP);
		noTabShownLabel.setText("Demo");
		showEmptyPage();

		ISelectionService selectionService = getSite().getWorkbenchWindow()
				.getSelectionService();
		selectionService.addSelectionListener(this);

		fillLocalToolBar();

	}

	private void showEmptyPage() {
		pagebook.showPage(noTabShownLabel);
	}
	
	private void showTabsPage(){
        pagebook.showPage(tabFolder);
    }

	public void setFocus() {
		tabFolder.setFocus();
	}

	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
        
	}

	public void fillLocalToolBar() {
		if (tabFolder != null) {
			IActionBars actionBars = getViewSite().getActionBars();

			// updateShellActions();
			// _clearAction = new ClearAction();
			// _printTableAction = new SystemTablePrintAction(getTitle(), null);
			IMenuManager menuManager = actionBars.getMenuManager();
			addMenuItems(menuManager);
			// _statusLine = actionBars.getStatusLineManager();

			IToolBarManager toolBarManager = actionBars.getToolBarManager();
			addToolBarItems(toolBarManager);

		}

	}

	private void addToolBarItems(IToolBarManager toolBarManager) {
		toolBarManager.removeAll();
		// if (_clearAction != null)
		// {
		// toolBarManager.add(_clearAction);
		// }
		// if (_shellActions != null && _shellActions.size() > 0)
		// {
		// for (int i =0; i < _shellActions.size(); i++)
		// {
		// Object shellAction = _shellActions.get(i);
		// if (shellAction instanceof IContributionItem)
		// {
		// toolBarManager.add((IContributionItem)shellAction);
		// }
		// else if (shellAction instanceof IAction)
		// {
		// toolBarManager.add((IAction)shellAction);
		// }
		// }
		// }
		IAction copy = new Action() {
			  public void run() {
				PlatformObject po =   new PlatformObject() {};
				 tabFolder.createTabItem((IAdaptable)po.getAdapter(ShellView.class), "Demo");
			  }
			  
			  public void runWithEvent(Event event) {
				  PlatformObject po =   new PlatformObject() {};
					CTabItem item =  tabFolder.createTabItem((IAdaptable)po.getAdapter(ShellView.class), "Demo");
					item.getControl().setData("Dehhlo");
					tabFolder.showPageFor(item.getText());
					showTabsPage();
					System.out.println("prabhatk: "+this.getClass().getName()+" "+item.toString());
			  }
		};
		copy.setText("Copy");
		IAction cut = new Action() {

		};
		cut.setText("Cut");
		IAction paste = new Action() {

		};
		
		paste.setText("Paste");
		toolBarManager.add(copy);
		toolBarManager.add(cut);
		toolBarManager.add(paste);
		toolBarManager.update(true);
	}

	private void addMenuItems(IMenuManager menuManager) {
		menuManager.removeAll();

		// IMenuManager launchMenu = new CommandMenuManager();

		// launchMenu.add(new SystemBaseDummyAction());
		// launchMenu.addMenuListener(this);
		// launchMenu.setRemoveAllWhenShown(true);
		// menuManager.add(launchMenu);
		menuManager.addMenuListener(this);
		IAction copy = new Action() {

		};
		copy.setText("Copy");
		IAction cut = new Action() {

		};
		cut.setText("Cut");
		IAction paste = new Action() {

		};
		paste.setText("Paste");
		menuManager.add(copy);
		menuManager.add(new Separator());
		menuManager.add(cut);
		menuManager.add(paste);
		menuManager.update(true);
		
	}

	public void widgetDefaultSelected(SelectionEvent e) {

	}

	public void widgetSelected(SelectionEvent e) {

	}

	public void menuAboutToShow(IMenuManager manager) {

	}

}
